package org.lex.utils.pool;

public abstract class AbstractPool<T> implements Pool<T> {
	static public final int DEFAULT_IDLE_NUMBER = 8;

	private volatile boolean closed = false;
	private volatile ObjectFactory<T> factory = null;

	public abstract T get();

	public abstract void put(T obj);

	public abstract void clear();

	public abstract int getActiveCount();

	public abstract int getPoolSize();

	public void close() {
		assertOpen();
		closed = true;
	}

	public boolean isClosed() {
		return closed;
	}

	public synchronized void setFactory(ObjectFactory<T> factory) {
		this.factory = factory;
	}

	public ObjectFactory<T> getFactory() {
		return this.factory;
	}

	protected void assertOpen() throws IllegalStateException {
		if (true == closed)
			throw new IllegalStateException("Pool closed.");
	}

	protected void assertFactory() throws NullPointerException {
		if (null == factory)
			throw new NullPointerException("Factory is null.");
	}
}
